package com.wh.wisdomsite.xbox.server.controller.ba;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.wh.wisdomsite.xbox.ba.entity.SysLog;
import com.wh.wisdomsite.xbox.ba.service.ISysLogService;
import com.wh.wisdomsite.xbox.common.util.ResManager;
import com.wh.wisdomsite.xbox.pojo.ba.SysLogIn;
import com.wh.wisdomsite.xbox.utils.StringUtils;
import com.wh.wisdomsite.xbox.utils.date.DateTool;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Map;


/**
 * 系统日志Controller
 *
 * @version V1.0
 * @Package: com.wh.wisdomsite.xbox.server.controller.us
 * @author: 丁奕
 * @date: 2018-08-08 17:33
 * @Copyright 公司版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2016
 **/
@Controller
@RequestMapping(value = "/sysLog")
public class SysLogController {

    private static final Log logger = LogFactory.getLog(SysLogController.class);

    @Autowired
    private ISysLogService iSysLogService;

    /**
     * 跳转页面到操作日志方法
     * @return
     */
    @RequiresPermissions("rules:ba_sysLog_page")
    @RequestMapping(value = "index.do", method = RequestMethod.GET)
    public String index(){
        return "/ba/sysLog/sysLogList";
    }

    /**
     * 获取用户日志列表
     * @param response
     * @param in
     */
    @RequiresPermissions("rules:ba_sysLog_find")
    @RequestMapping(value = "/sysLogList.do")
    public void SysLogList(HttpServletResponse response, SysLogIn in){
        try {
            logger.info("加载用户日志列表-->>开始");
            //设置分页
            if(in.getPage() == 0) in.setPage(1);
            if(in.getRows() == 0) in.setRows(10);
            Page<SysLog> page = new Page<>();
            page.setCurrent(in.getPage());
            page.setSize(in.getRows());
            EntityWrapper<SysLog> ewSysLog = new EntityWrapper<SysLog>();
            ewSysLog.setEntity(new SysLog());
            if(StringUtils.isNotEmpty(in.getTitle())){
                ewSysLog.like("sl.title",in.getTitle());
            }
            if(StringUtils.isNotEmpty(in.getType())){
                ewSysLog.eq("sl.type",in.getType());
            }
            if(StringUtils.isNotEmpty(in.getStartTime()) && StringUtils.isNotEmpty(in.getEndTime())){
                ewSysLog.between("sl.createDate", in.getStartTime(), in.getEndTime());
            }
            ewSysLog.orderBy("sl.createDate",false);
            Page<Map<String, Object>> sysLogPages = iSysLogService.getMapsPage(page,ewSysLog);
            JSONObject json = new JSONObject();
            JSONArray jsonArray = new JSONArray();
            if(sysLogPages.getRecords().size() > 0){
                for(Map<String, Object> sysLog : sysLogPages.getRecords()){
                    JSONObject object = new JSONObject(sysLog);
                    object.put("createBy", sysLog.get("createBy") + "");
                    object.put("operatName", sysLog.get("title") + "");
                    object.put("createDate", DateTool.format((Date)sysLog.get("createDate"), "yyyy-MM-dd HH:mm:ss"));
                    jsonArray.put(object);
                }
            }
            json.put("total", sysLogPages.getTotal());
            json.put("rows", jsonArray);
            ResManager.getTextOut(response).print(json);
            logger.info("加载用户日志列表-->>结束");
        }catch (Exception e){
            e.printStackTrace();
            logger.debug("加载用户日志列表-->>Exception" + e.getMessage());
        }
    }

    /**
     * 用户日志详情
     * @param id
     * @param request
     * @return
     */
    @RequiresPermissions("rules:ba_sysLog_view")
    @RequestMapping("sysLogDesc.do")
    public String sysLogDesc(String id, HttpServletRequest request) {
        logger.info("用户日志详情-->>开始");
        Map<String , Object> sysLog = iSysLogService.getMapById(id);
        String createDate = DateTool.format((Date)sysLog.get("createDate"), "yyyy-MM-dd HH:mm:ss");
        request.setAttribute("sysLog", sysLog);
        request.setAttribute("createDate", createDate);
        logger.info("用户日志详情-->>结束");
        return "ba/sysLog/sysLogDesc";
    }
}
